<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/validate.jsp"%>
<%
/**
**写病人预约信息
cardNo	  身份证号
pbkm	    获取科室信息的pbkm 或者 获取排班医生信息的pbkm
deptName  获取科室信息的name 或者 获取排班医生信息的name
yylx	    预约类型：预约科室1,预约医生0
pbmxid	  获取医生预约信息的pbmxid 或者 获取科室预约信息的pbmxid
yyrq	    预约日期20180529
yyTime	  预约时间：’07:45-11:30’,’13:00-16:30’,’00:00-23:59’
*/
try {
	String cardNo   = Tools.getDesc(input.getDesc("cardNo",""));    
    List<UserProfile> userProfiles = QBuilder.db("basedb").select().from("UserProfile").where(new Where("hospital=", hospital).and("status = ", STATUS_ENABLE).and("card_number=", cardNo)).query().getResultObj();
    if (isEmpty(userProfiles)) {
        Tools.encryptPrint(out, new Message(30041, "病人信息不存在"));
        return ;
    }
    UserProfile userProfile = userProfiles.get(0);
	String pbkm     = Tools.getDesc(input.getDesc("pbkm", ""));            
	String deptName     = Tools.getDesc(input.getDesc("deptName", ""));          
	String yylx     = Tools.getDesc(input.getDesc("yylx", ""));   	       
	String pbmxid     = Tools.getDesc(input.getDesc("pbmxid", ""));           
	String yyTime     = Tools.getDesc(input.getDesc("yyTime", ""));    

	
	/**
	**作用：前端传人格式yyyy-MM-dd转yyyyMMdd
	*目的：为了与数据库原有数据格式保持一致
	*/
	String yyrq = input.getDesc("yyrq", "");
	String regEx="[^0-9]";  
	Pattern p = Pattern.compile(regEx);  
	Matcher m2 = p.matcher(yyrq);  
	yyrq=m2.replaceAll("").trim();            

	
	//写病人预约信息
	Map<String, String> param  = new HashMap<String,String>(16);
	param.put("cardNo",cardNo); 
	param.put("pbkm",pbkm);                 
	param.put("deptName",deptName);         
	param.put("yylx",yylx);             
	param.put("pbmxid",pbmxid);    
	param.put("yyrq",yyrq);    
	param.put("yyTime",yyTime);               
	
	
	//param.put("hospital",String.valueOf(hospital)); 
/* 	System.out.println("cardNo     :"+cardNo    );    
	System.out.println("pbkm     :"+pbkm    );    
	System.out.println("deptName :"+deptName);    
	System.out.println("yylx     :"+yylx    );    
	System.out.println("pbmxid   :"+pbmxid  );    
	System.out.println("yyTime   :"+yyTime  );    
	System.out.println("yyrq   :"+yyrq  );  */
	
	
	// 调用realnamejudge查询是否有记录
	String rnUrl = HOST_BASE_URL_1113 + "/appoint/realnamejudge.jsp";
	Map<String, String> rnParam  = new HashMap<String,String>(2);
	rnParam.put("cardNumber",cardNo);
	String rnRet = HttpUtil.post(rnUrl,rnParam);
    Map<Object, Object> map =  JSONUtil.mapFromJson(rnRet);
    int myErrCode = (Integer)map.get("errcode");
    boolean flag = false;
    if(hospital == 1113 && myErrCode==30041) {
		/**
		**增加病人信息 
		**/
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		int seed = new Random().nextInt(999999);
		seed = seed < 100000 ? (seed += 100000): seed;
		String sickName        =  userProfile.getName();     // 姓名        
		String cardNum        =  String.valueOf(sdf.format(new Date())+seed);   //卡号 
		String sfzh          =  cardNo; // 身份证号
		String sex           =  IdCardUtil.getGender(sfzh)%2==0?"女":"男";      //性别  
		String birth         =  sfzh.substring(6, 14);    // 出生日期  
		String lxdz          =  "暂无";     //联系地址（必填）
		String lxdh 		 =	userProfile.getPhone(); //联系电话（必填）  

		param.put("sickName",sickName);                        
		param.put("cardNum",cardNum);                             
		param.put("sfzh",sfzh);                 
		param.put("sex",sex); 
		param.put("birth",birth);               
		param.put("lxdz",lxdz);                 
		param.put("lxdh",lxdh);                   
	    String url = HOST_BASE_URL_1113 + "/appoint/addpatientinfo.jsp";
	    String ret = HttpUtil.post(url,param);
	    msg.setErrcode(30041);
		msg.setErrmsg("预约失败，请重新预约。");
	    Tools.encryptPrint(out,JSONUtil.toJson(msg));
	    flag = true;
	    return;
    }
    
    
    if(!flag) {
        String url = HOST_BASE_URL_1113 + "/appoint/addpatientappoint.jsp";
        String json = HttpUtil.post(url,param);
        Tools.encryptPrint(out,json);
    }
} catch (Exception e){
	e.printStackTrace();
	msg.put("errcode",FAILURE);
}

%>
